<?php

namespace Hdphim\Modules\User\Controllers;

use Phalcon\Mvc\Controller as Controller;
use Hdphim\Modules\User\Forms\SignUpForm;
use Hdphim\Modules\User\Forms\LoginForm;
use Hdphim\Modules\User\Models\Users;

class UserController extends Controller
{
    public function indexAction()
    {
        /*$this->getDI()->getMail()->send(
            array(
                $this->user->getEmail() => $this->user->getName()
            ),
            'Please confirm your email',
            'confirmation',
            array(
                'confirmUrl' => '/user/confirmEmail/' . $this->getCode(). '/' . $this->user->getEmail()
            )
        );
        $mail = $this->mail;    
        $mail->send(
            array(
                'supermouseno1@gmail.com' => 'Huy Nguyen'
            ),
            'Please confirm your email',
            'confirmation',
            array(
                'confirmUrl' => 'ok men'
            )
        );*/
        
    }
    
    public function loginAction()
    {
       
        $this->tag->setTitle('Login');
        
        if($this->auth->isUserSignedIn())
        {
            return $this->response->redirect();
        }
        
        $form = new LoginForm();
      
        try {
            if (!$this->request->isPost()) {
                
            } else {
                if ($form->isValid($this->request->getPost()) == false) 
                {
                    foreach ($form->getMessages() as $message) {
                        $this->flash->error($message);
                    }
                } 
                else 
                {
                    $this->auth->check(array(
                        'email' => $this->request->getPost('email'),
                        'password' => $this->request->getPost('password'),
                        'remember' => $this->request->getPost('remember')
                    ));

                    return $this->response->redirect('user/index');
                }
            }
        } catch (\Exception $e) {
            $this->flash->error($e->getMessage());
        }
        
        $this->view->form = $form;        
    }
    
    public function signupAction()
    {
        $this->tag->setTitle('Signup');
    
        $form = new SignUpForm();
        if($this->request->isPost())
        {
            if($form->isValid($this->request->getPost()) != false)
            {
                $user = new Users();
                
                $user->assign(array(
                    'name' => $this->request->getPost('name'),
                    'email' => $this->request->getPost('email'),
                    'password' => $this->security->hash($this->request->getPost('password')),
                    'group_id' => 2,
                    'banned' => 'N',
                    'suspended' => 'N',
                    'active' => 'N',
                    'created_at' => date("Y-m-d H:i:s"),
                    'updated_at' => date("Y-m-d H:i:s")
                ));

                if ($user->save()) {

                    $userProfiles = new UsersProfile();
                    $userProfiles->assign(array(
                        'users_id' => $user->getId(),
                        'created_at' => date("Y-m-d H:i:s"),
                        'updated_at' => date("Y-m-d H:i:s")
                    ));

                    $userProfiles->save();

                    return $this->dispatcher->forward(array(
                            'controller' => 'user',
                            'action' => 'index'
                    ));
                }
                
                if($user->save())
                {
                	
                    return $this->dispatcher->forward(array(
                        'controller' => 'index',
                        'action' => 'index'
                    ));
                }
                $this->flash->error($user->getMessages());
            }
        }

        $this->view->form = $form;
    }

    public function logoutAction()
    {
        $this->auth->remove();
        return $this->response->redirect();
    }
}
